<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Vehicle extends Controller {

    function setVars()
    {
		$this->tpl['title'] = 'Billboard Vehicle';
        $this->tpl['head'] = '';
        $this->tpl['Title']  = '';
        $this->tpl['check']  = '';
        $this->tpl['Image']  = '';
        $this->tpl['error']  = '';
		$this->uid = $this->session->userdata('uid');
        $this->dbtable = 'bb_vehicle';
	}
    
	function __construct()
	{
        parent::Controller();
        $this->auth->CheckTaMeR(11,100);
        $this->load->model('Tamer_model', 'tm');
        $this->load->library('upload');
		$this->setVars();
        log_message('debug', "Billboard: Controler Class Initialized");
	}
    
	function index() # Display vehilce list
	{
		$this->vehicle_list();
	}

	function addImage()
	{
        $this->load->view('billboard/upload_form',  $this->tpl );        
    }
    
	function uploadImage()
	{
        $id = $this->uri->segment(4);
        $name = $this->uri->segment(5);
        $config['upload_path'] = 'img/vehicle/';
        $config['overwrite'] = TRUE;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name'] = TRUE;
        $config['max_size']	= '1024'; # 1024 = 1MB
        $config['max_width']  = '800';
        $config['max_height']  = '600';
        $this->upload->initialize($config);
        
        if ( ! $this->upload->do_upload()){
            $this->tpl['error'] = $this->upload->display_errors();
            $this->load->view('billboard/upload_form', $this->tpl);
        }else{
            $this->tpl['upload_data'] = $this->upload->data();
            $new_file = $this->tpl['upload_data']['file_path'];
            $new_file .= $name;
            $new_file .= $this->tpl['upload_data']['file_ext'];
            rename($this->tpl['upload_data']['full_path'], $new_file);
            $config['image_library'] = 'GD2';
            $config['source_image'] = $new_file;
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 100;
            $config['height'] = 100;
            $this->load->library('image_lib', $config);
            if ( ! $this->image_lib->resize()){
                $error = $this->image_lib->display_errors();
                $this->msg->setError($error);
            }else{
                $config['create_thumb'] = FALSE;
                $config['width'] = 500;
                $config['height'] = 500;
                $this->image_lib->initialize($config);
                if ( ! $this->image_lib->resize()){
                    $error = $this->image_lib->display_errors();
                    $this->msg->setError($error);
                }else{
                    $img = $name.'_thumb'.$this->tpl['upload_data']['file_ext'];
                    $this->tm->save(array('image'=> $img), $id);
                }
            }
            $this->load->view('billboard/upload_success', $this->tpl);
            //redirect('/billboard/vehicle/vehicle_list/', 'location');
        }
	}	

	function vehicle_add_edit()
	{
        if( ! isset($_POST['submit_vehicle'] ) ) {
            if($id = $this->uri->segment(4)){
                $row = $this->tm->getVehicle( $id );
                $this->session->set_flashdata( 'vid', $id );
            }else{
                $row = $this->tm->getFields();
            }

            foreach($row as $k=>$v){
                $this->tpl[$k] = $v;
            }
            $this->load->view('billboard/vehicle_add', $this->tpl);
        }else{
            if($this->oid < 1){
                redirect('dbview/pages/error_company_info', 'location');
            }
            $id = $this->session->flashdata( 'vid' );
            $this->load->library('validation');
            $rules['Name']			= "trim|required|min_length[4]|max_length[30]|alpha_dash";
            $rules['Showroom']  	= "trim|required|max_length[3]";
            $this->validation->set_rules($rules);
            if ($this->validation->run() == FALSE)
            {
                $this->session->keep_flashdata( 'vid' );
                $this->load->view('billboard/vehicle_add', $this->tpl);
            }
            else
            {
                $this->load->helper('date');
                $time = time();
                $GMTMySqlString = gmdate("Y-m-d", $time);
                $data = array(
                    'id' => $id,
                    'Name' => $this->validation->Name ,
                    'Showroom' => $this->validation->Showroom ,
                    'first' => $GMTMySqlString,
                    'org_id' => $this->oid
                    );
                
                if( FALSE !== $this->tm->save($data)){
                    redirect('billboard/vehicle/vehicle_list', 'location');
                }else{
                    $this->load->view('billboard/vehicle_add', $this->tpl);
                    //redirect('dbview/pages/error_db', 'location');
                }
            }
        }
    }    
    
	function vehicle_add_editOLD()
	{
        if($id = $this->uri->segment(4)){
            $row = $this->tm->getVehicle( $id );
            $this->session->set_flashdata( 'vid', $id );
        }else{
            $row = $this->tm->getFields();
        }
        if( ! isset($_POST['submit_vehicle'] ) ) {
            foreach($row as $k=>$v){
                $this->tpl[$k] = $v;
            }
            $this->load->view('billboard/vehicle_add', $this->tpl);
        }else{
            if($this->oid < 1){
                redirect('dbview/pages/error_company_info', 'location');
            }
            $id = $this->session->flashdata( 'vid' );
            $this->load->library('validation');
            $rules['Name']			= "trim|required|min_length[4]|max_length[30]|alpha_dash";
            $rules['Showroom']  	= "trim|required|max_length[3]";
            $this->validation->set_rules($rules);
            if ($this->validation->run() == FALSE)
            {
                $this->session->keep_flashdata( 'vid' );
                $this->load->view('billboard/vehicle_add', $this->tpl);
            }
            else
            {
                $this->load->helper('date');
                $time = time();
                $GMTMySqlString = gmdate("Y-m-d", $time);
                $data = array(
                    'id' => $id,
                    'Name' => $this->validation->Name ,
                    'Showroom' => $this->validation->Showroom ,
                    'first' => $GMTMySqlString,
                    'org_id' => $this->oid
                    );
                
                if( FALSE !== $this->tm->save($data)){
                    redirect('billboard/vehicle/vehicle_list', 'location');
                }else{
                    $this->load->view('billboard/vehicle_add', $this->tpl);
                    //redirect('dbview/pages/error_db', 'location');
                }
            }
        }
    }    

    function vehicle_list()
    {
        $this->db->select('id, image, Name, Showroom');
        $query = $this->db->getwhere('billboard_vehicle', array('org_id' => $this->oid));
        if ($query->num_rows() > 0)
        {
            foreach($query->result_array() as $k => $v) $tbresult[$k] = $v;
            $this->load->library('table');
            $this->table->set_heading('Photo', 'Name', 'Showroom', 'Select');
            foreach($tbresult as $row){
                $v = $row['id'];
                $n = $row['Name'];
                $img = $row['image'];
                $this->load->helper('img');
                $row['image'] = '<img src="/img/vehicle/'.$img.'" alt="n/a" />';
                $row['view'] = anchor_icon('billboard/vehicle/vehicle_view/'.$v,'actions/edit-find', 'View');
                $row['view'] .= ' | ' . anchor_icon('billboard/vehicle/vehicle_add_edit/'.$v,'actions/document-new','Add/Edit');
                $row['view'] .= ' | ' . anchor_icon('billboard/vehicle/addImage/'.$v.'/'.$n, 'devices/camera-photo');
                unset($row['id']);
                $this->table->add_row($row);
            }            
            $this->tpl['body'] = $this->table->generate();
            $this->table->clear();
        }
        $this->load->view('blank', $this->tpl);
    }
    
    function vehicle_view()
    {
        $vid = $this->uri->segment(4);
        $this->db->select('id, Name, Showroom, image');
        $query = $this->db->getwhere('billboard_vehicle', array('id' => $vid));
        if ($query->num_rows() > 0)
        {
            $v = $query->row();
            $this->load->library('table');

            $form['Image'] = '<img src="/img/vehicle/'.$v->image.'" alt="n/a" />';
            $form[1]['id'] = $v->id;
            $form[1]['Name'] = $v->Name;
            $form[1]['Showroom'] = $v->Showroom;
            $this->tpl['sidebar'] = $this->table->form_table($form);
            $img = str_replace('_thumb','', $v->image);
            $this->tpl['body'] = '<img src="/img/vehicle/'.$img.'" alt="n/a" />';
            
            
        }
        $this->load->view('blank', $this->tpl);
    }
    
}
?>
